package com.zhangpeng520.attend.controller;

import com.zhangpeng520.attend.dao.AttendMapper;
import com.zhangpeng520.attend.entity.Attend;

import com.zhangpeng520.attend.service.AttendService;
import com.zhangpeng520.common.PageObject.QueryPageBean;
import com.zhangpeng520.common.PageObject.QueryPageCondition;
import com.zhangpeng520.common.date.DateUtils;
import com.zhangpeng520.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
@RequestMapping("attend")
public class attendController {

    @Autowired
    private AttendService attendService;

    private  static SimpleDateFormat DateFormat=new SimpleDateFormat("yy-MM-dd");
    private  static SimpleDateFormat TimeFormat=new SimpleDateFormat("HH-mm-ss");

    final int NOON_HOURS=12;
    final int NOON_MINUTES=00;
    final byte ATTEND_NORMAL=1;
    final byte ATTEND_ABNORMAL=2;


    /**
     *
     *@Author zhangpeng(www.zhangpeng520.com)
     *@Date 2017/8/15 0015 19:47
     *@Desripition进入打卡页面
     *
     */
    @RequestMapping
    public String getAttend()
    {

        return "attends";
    }


    /**
     *
     *@Author zhangpeng(www.zhangpeng520.com)
     *@Date 2017/8/15 0015 19:46
     *@Desripition 打卡记录
     *
     */
    @ResponseBody
    @RequestMapping("/attend")
    public void  attend(HttpServletRequest request)
    {
        Date todayDate= new Date();
        TimeFormat.format(todayDate);

        //中午12点
        Date noonTime=DateUtils.getDate(NOON_HOURS,NOON_MINUTES);
        User user=(User)request.getSession().getAttribute("loginInfo");
        Attend attend=attendService.findAttend(user.getId());
        if(attend==null)//每天第一次打卡
        {
            attend=new Attend();
            attend.setAttendDate(todayDate);
            attend.setUserId(user.getId());
            attend.setAttendWeek((byte)DateUtils.getTodayWeek());
            attend.setStatus(ATTEND_NORMAL);
            if(todayDate.compareTo(noonTime)<=0)//判断打卡时间是在上午还是下午
            {
                attend.setAttendMoring(todayDate);
            }
            else
            {
                attend.setAttendEvening(todayDate);
            }
             attendService.signAttend(attend);
        }
        else
        {
            /**
             *
             * 如果不是第一次打卡，如果是上午打卡，则打卡时间依然保留最早一次打卡时间，
             * 如果是下午打卡，则把打卡时间更新为当前时间
             */
            attend.setAttendWeek((byte)DateUtils.getTodayWeek());
            if(todayDate.compareTo(noonTime)<=0)
            {
                return;
            }
            else
            {
                if(todayDate.compareTo(attend.getAttendEvening())>0) {
                    attend.setAttendEvening(todayDate);
                }

            }
            attendService.updateAttend(attend);

        }


    }

  /**
   *
   *@Author zhangpeng(www.zhangpeng520.com)
   *@Date 2017/8/17 0017 10:14
   *@Desripition 获取分页数据
   *
   */
    @ResponseBody
    @RequestMapping("/devPage")
    public QueryPageBean getAttendDate(QueryPageCondition condition,HttpServletRequest request)
    {

         User user= (User)request.getSession().getAttribute("loginInfo");
         String[] rangeDate=condition.getRangeDate().split("/");
         condition.setUserId(user.getId());
         condition.setStartDate(rangeDate[0]);
         condition.setEndDate(rangeDate[1]);
         condition.setAttendStatus(condition.getAttendStatus());
         QueryPageBean attendCondition=attendService.findAttendList(condition);

        return attendCondition;
    }
}

